﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace PookSistener.SystemConfig
{
    public partial class frmSubCategoryAdd : Form
    {
        internal string MODE = "";

        OleDbConnection conn = null;

        public frmSubCategoryAdd()
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (MODE.Equals("ADD"))
            {
                string sql = "INSERT INTO SubCategory (Name) VALUES (@Name)";

                OleDbCommand cmd = new OleDbCommand();
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = sql;
                cmd.Connection = conn;

                cmd.Parameters.Clear();
                cmd.Parameters.Add("@Name", OleDbType.VarChar).Value = txtName.Text;

                cmd.ExecuteNonQuery();

                MessageBox.Show("เพิ่มข้อมูลเรียบร้อยแล้ว");
                txtName.Text = "";
            }
            else if (MODE.Equals("EDIT"))
            {
                string sql = "UPDATE SubCategory SET Name=@Name WHERE ID=@ID";

                try
                {
                    int id = Convert.ToInt32(txtID.Text);

                    OleDbCommand cmd = new OleDbCommand();
                    cmd.CommandType = CommandType.Text;
                    cmd.CommandText = sql;
                    cmd.Connection = conn;

                    cmd.Parameters.Clear();
                    cmd.Parameters.Add("@Name", OleDbType.VarChar).Value = txtName.Text;
                    cmd.Parameters.Add("@ID", OleDbType.Integer).Value = id;

                    int affected_row = cmd.ExecuteNonQuery();

                    MessageBox.Show("แก้ไขข้อมูลเรียบร้อยแล้ว");

                    this.Close();

                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error : " + ex.Message);
                }
            }
        }

        private void frmSubCategoryAdd_Load(object sender, EventArgs e)
        {
            conn = new OleDbConnection(PookSistener.Properties.Settings.Default.DatabaseConnectionString);
            if (conn.State == ConnectionState.Open)
            {
                conn.Close();
            }

            conn.Open();
        }
    }
}
